package com.yc.snack.util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 后台过滤器
 * @author lenovo
 *
 */
@WebFilter(filterName="CheckAuthorFilter" ,value="/back/manager/*")
public class CheckAuthorFilter implements Filter {

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		//火球session ,从session 中获取登录用户信息
		HttpServletRequest req = (HttpServletRequest)request;
		HttpServletResponse resp = (HttpServletResponse) response;
		Object obj = req.getSession().getAttribute(ConstantInfo.CURRENTLOGINADMIN);
		
		//判空，则直接返回，当前请求到此结束
		if(obj == null) {
			resp.setContentType("text/html;charset=utf8");
			PrintWriter out = resp.getWriter();
			out.print("<script>alert('请先登录...');location.href='" + req.getContextPath() + "/back/login.html'</script>");
			out.flush();
			return;
		}
		
		//如果满足请求则交给下一个过滤器
		chain.doFilter(req, resp);

	}




}
